package com.yuxin.tongyi.lsll.main;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import com.yuxin.tongyi.lsll.R;
import com.yuxin.tongyi.lsll.common.Constants;
import com.yuxin.tongyi.lsll.sub.CaremaPhotoUploadActivity;
import com.yuxin.tongyi.lsll.sub.DayLifeActivity;
import com.yuxin.tongyi.lsll.sub.NotificationActivity;
import com.yuxin.tongyi.lsll.util.PubUtil;


public class MainActivity extends Activity {
	private PowerManager powerManager;
	private WakeLock wakeLock;
	
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        this.powerManager = (PowerManager)this.getSystemService(Context.POWER_SERVICE);         
		this.wakeLock = this.powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Lock");
		wakeLock.acquire();
		
        GridView gridView = (GridView) findViewById(R.id.main_gridview);
        ArrayList<HashMap<String, Object>> arrImageItem = getData();
        
        SimpleAdapter adapter = new SimpleAdapter(this, arrImageItem, R.layout.imageitem, 
        										  new String[]{Constants.ITEM_IMAGE, Constants.ITEM_TEXT},
        										  new int[]{R.id.main_ivImage, R.id.main_tvItemName});
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(new ItemClickListener());
    }
	
	public ArrayList<HashMap<String, Object>> getData(){
		ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String,Object>>();
		HashMap<String, Object> map = null;

		map = new HashMap<String, Object>();
		map.put(Constants.ITEM_IMAGE, R.drawable.main_menu_babyphoto);
		map.put(Constants.ITEM_TEXT, getResources().getString(R.string.caremaPhoto));
		list.add(map);
		
//		map = new HashMap<String, Object>();
//		map.put(Constants.ITEM_IMAGE, R.drawable.main_menu_babyphoto);
//		map.put(Constants.ITEM_TEXT, getResources().getString(R.string.uploadPhoto));
//		list.add(map);
		
		map = new HashMap<String, Object>();
		map.put(Constants.ITEM_IMAGE, R.drawable.main_menu_daylife);
		map.put(Constants.ITEM_TEXT, getResources().getString(R.string.dayLife));
		list.add(map);
		
		map = new HashMap<String, Object>();
		map.put(Constants.ITEM_IMAGE, R.drawable.main_menu_notification);
		map.put(Constants.ITEM_TEXT, getResources().getString(R.string.notification));
		list.add(map);
		return list;
	}
	
	
	
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, Constants.MENU_ITEM_EXIT, 1, "退出系统");
		menu.add(0, Constants.MENU_ITEM_RELOGIN, 2, "重新登录");
		return super.onCreateOptionsMenu(menu);
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case Constants.MENU_ITEM_EXIT:
			//退出系统时，删除SharedPreferences存储的xml文件
			removeSharedPreferences();
			System.exit(0);
			break;
		case Constants.MENU_ITEM_RELOGIN:
			Intent intent = new Intent(MainActivity.this, LoginActivity.class);
			intent.putExtra(Constants.LOGIN_KEY_FLAG, Constants.LOGIN_VALUE_RE);
			startActivity(intent);
			//重新登录后结束该Activity，确保在重新登录界面退出系统后不会再返回到该Activity
			this.finish();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode==KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0){
			AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
			alertDialog.setTitle("提示")
					   .setMessage("是否退出程序？")
					   .setPositiveButton("确定", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							//退出系统时，删除SharedPreferences存储的xml文件
							removeSharedPreferences();
							System.exit(0);
						}})
					   .setNegativeButton("取消", null)
					   .show();
		}
		return super.onKeyDown(keyCode, event);
	}

	class ItemClickListener implements OnItemClickListener{

		public void onItemClick(AdapterView<?> adapterView, View view, int position,
				long rowid) {
			//子菜单界面
			Intent intent = null;
			switch (position) {
			case 0:
				intent = new Intent(MainActivity.this,CaremaPhotoUploadActivity.class);
				break;
//			case 1:
//				intent = new Intent(MainActivity.this, StorePhotoUploadActivity.class);
//				break;
			case 1:
				intent = new Intent(MainActivity.this, DayLifeActivity.class);
				break;
			case 2:
				intent = new Intent(MainActivity.this, NotificationActivity.class);
				break;
			default:
				break;
			}
			
			startActivity(intent);
		}
		
	}
	
	public void removeSharedPreferences(){
		//删除SharedPreferences存储的xml文件中短信发送信息
		PubUtil.removeConfigValue(MainActivity.this, Constants.CFG_MESSAGE_CLASS_ID);
		PubUtil.removeConfigValue(MainActivity.this, Constants.CFG_MESSAGE_CLASS_NAME);
		PubUtil.removeConfigValue(MainActivity.this, Constants.CFG_MESSAGE_CONTENT);
	}
	
}